package com.qire.manhua.controller;

import android.view.View;
import android.widget.EditText;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.OSUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.activity.MessageCommentDetailActivity;
import com.qire.manhua.activity.helper.LoginRouter;
import com.qire.manhua.databinding.ActivityMessageCommentDetailBinding;
import com.qire.manhua.dialog.ComicCommentOptionMenu;
import com.qire.manhua.dialog.CommentComplaintDialog;
import com.qire.manhua.model.viewModel.impl.MessageCommentDetailViewModel;
import com.qire.manhua.model.viewModel.impl.MessageCommentDetailViewModel.MessageCommentItemViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import net.yslibrary.android.keyboardvisibilityevent.util.UIUtil;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class MessageCommentDetailController extends EventController<MessageCommentDetailActivity> implements AutoVariable {

    private MessageCommentDetailViewModel viewModel;

    public MessageCommentDetailController(@NonNull MessageCommentDetailActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityMessageCommentDetailBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setMessageCommentDetailController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.messageCommentDetailController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(MessageCommentDetailViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadReplyData();
    }
    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout){
        viewModel.loadMoreReplyData();
    }

    public void backBtnOnClick(View view){
        owner.onBackPressed();
    }

    public void gotoComicOnClick(View view) {
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                .withData("comicBookId", viewModel.comicBookId())
                .navigation();
    }

    /**
     * 回复评论按钮单击事件
     * @param view
     */
    public void replyCommentBtnOnClick(View view){
        if(LoginRouter.ROUTER.checkLoginRouteIfNeed()) {
            return;
        }
        if(viewModel.submitReply()) {
            UIUtil.hideKeyboard(owner);
        }
    }

    /**
     * 回复评论编辑框单击事件
     * */
    public void replyCommentEditOnClick(View view){
        ((EditText)view).setCursorVisible(true);
    }

    //region #评论子项按钮事件 begin

    /**
     * 选择回复评论目标点击事件
     * @param view
     */
    public void pickReplyTargetOnClick(View view) {
        MessageCommentItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), MessageCommentItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            viewModel.setReplyToTarget(itemViewModel);
            UIUtil.hideKeyboard(owner);
        }
    }

    /**
     * 评论的长按事件
     * */
    public boolean commentItemOnLongClick(View view) {
        final MessageCommentItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), MessageCommentItemViewModel.class);
        if(ObjectUtil.isNull(itemViewModel)) {
            return false;
        }
        ComicCommentOptionMenu menu = new ComicCommentOptionMenu(view.getContext());
        menu.setComplaintListener(v -> {
            v.setTag(itemViewModel);
            complaintBtnOnClick(v);
            v.setOnClickListener(null);
        }).setReplyListener(v -> {
            v.setTag(itemViewModel);
            pickReplyTargetOnClick(v);
            v.setOnClickListener(null);
        }).setCopyListener(v ->{
            String text = itemViewModel.content();
            OSUtil.copyToClipboard(view.getContext(), "Label", text);
            ToastHelper.showToast("复制成功");
            v.setOnClickListener(null);
        }).show();
        return true;
    }

    /**
     * 评论的举报按钮单击事件
     * */
    public void complaintBtnOnClick(View view) {
        MessageCommentItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), MessageCommentItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            CommentComplaintDialog dialog = new CommentComplaintDialog(view.getContext());
            dialog.setCommentInfo(itemViewModel.userName(), itemViewModel.content());
            dialog.setListener((int number, String text) -> itemViewModel.complaint(number, text));
            dialog.show();
        }
    }

    /**
     * 评论的点赞按钮单击事件
     * */
    public void giveThumbsUpBtnOnClick(View view) {
        MessageCommentItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), MessageCommentItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            if(itemViewModel.isMyLike()) {
                ToastHelper.showToast("您已经点过赞了");
                return;
            }
            itemViewModel.giveAlike();
        }
    }

    //endregion #评论子项按钮事件 end

}
